package com.sm42.goodsController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sm42.baseController.BaseCrmpController;
import com.sm42.common.http.AxiosResult;
import com.sm42.common.page.PageBean;
import com.sm42.goodsEntity.Goods;
import com.sm42.goodsService.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("good")
public class GoodsController extends BaseCrmpController<Goods> {
    @Autowired
    private GoodsService goodsService;

    /**
     * 模糊查询
     */
    @GetMapping("findSearch")
    public AxiosResult findSearch(Goods goods,Integer pageSize,Integer pageNum) {
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        goodsService.findSearch(goods);
        return AxiosResult.success(PageBean.initData(page.getTotal(),page.getResult()));
    }


    /**
     * 查询所有
     */
    @GetMapping("findAllGoods")
    public AxiosResult<List<Goods>> findAllGoods() {
        return AxiosResult.success(goodsService.findAllList());
    }

    /**
     * 新增数据
     */
    @GetMapping("addGoods")
    public AxiosResult addGoods(Goods goods) {
        return AxiosResult.success(goodsService.addGoods(goods));
    }
    /**
     * 修改数据
     */
    @PostMapping("updateGoods")
    public AxiosResult updateGoods(Goods goods) {
        System.out.println(goods);
        return AxiosResult.success(goodsService.updateGoods(goods));
    }
    /**
     * 根据id删除
     */
    @GetMapping("deleteGoods")
    public AxiosResult deleteGoods(Long id){
        return AxiosResult.success(goodsService.deleteGoods(id));
    }
}
